package com.sciencep.pro.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sciencep.pro.common.config.Constant;
import com.sciencep.pro.core.domain.dto.project.ProDataTO;
import com.sciencep.pro.core.domain.dto.user.UserDetail;
import com.sciencep.pro.core.domain.entity.ProBaseInfo;
import com.sciencep.pro.core.domain.entity.ProData;
import com.sciencep.pro.core.dao.ProDataMapper;
import com.sciencep.pro.core.domain.entity.ProNode;
import com.sciencep.pro.core.service.IProBaseInfoService;
import com.sciencep.pro.core.service.IProDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sciencep.pro.core.service.IProNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Sciencep
 */
@Service
public class ProDataServiceImpl extends ServiceImpl<ProDataMapper, ProData> implements IProDataService {

    @Autowired
    @Lazy
    IProBaseInfoService proBaseInfoService;

    @Autowired
    IProNodeService proNodeService;

    @Override
    public boolean addProDate(ProDataTO proDataTO, UserDetail userDetail) {
        ProData proData = new ProData();
        proData.setProId(Long.valueOf(proDataTO.getProId()));
        proData.setProDataId(proDataTO.getProDataId());
        proData.setProNodeId(proDataTO.getProNodeId());
        proData.setMustFlag(proDataTO.getMustFlag());
        proData.setGitAdr(proDataTO.getGitAdr());
        proData.setCreateUserId(userDetail.getUserId());
        proData.setCreateUserName(userDetail.getUserName());
        return this.save(proData);
    }

    @Override
    public boolean updateProDate(ProDataTO proDataTO, UserDetail userDetail) {
        ProData proData = new ProData();
        proData.setId(Long.valueOf(proDataTO.getId()));
        proData.setProId(Long.valueOf(proDataTO.getProId()));
        proData.setProDataId(proDataTO.getProDataId());
        proData.setProNodeId(proDataTO.getProNodeId());
        proData.setMustFlag(proDataTO.getMustFlag());
        proData.setGitAdr(proDataTO.getGitAdr());
        proData.setModifyUserId(userDetail.getUserId());
        proData.setModifyUserName(userDetail.getUserName());
        return this.updateById(proData);
    }

    @Override
    public List<ProNode> findProNodeById(String proId) {
        ProBaseInfo proBaseInfo = proBaseInfoService.getOne(new LambdaQueryWrapper<ProBaseInfo>().eq(ProBaseInfo::getId, proId));
        return proNodeService.list(new LambdaQueryWrapper<ProNode>().eq(ProNode::getTypeId, proBaseInfo.getProTypeId()).eq(ProNode::getIsDel, Constant.IsDelete.NO).orderByAsc(ProNode::getId));
    }
}
